package cn.lx.service;

import cn.lx.entity.User;
import cn.lx.mapper.UserMapper;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author lixin
 */
@Service
public class UserService {


    @Autowired
    private UserMapper userMapper;

    public List<User> getUsers() {
        return userMapper.getUsers();
    }


    @CachePut(cacheNames = "user", key = "#user.userName")
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @CachePut(cacheNames = "user", key = "#user.userName")
    public List<User> editUser(User user) {
        userMapper.editUser(user);
        return null;
    }

    @Cacheable(value = "user", key = "#userName")
    public User getUsersByName(String userName) {
        User user = userMapper.getUsersByName(userName);
        System.out.println("从数据库读取，而非读取缓存！");
        return user;
    }

}
